//
//  NewsCommentCell.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "NewsCommentCell.h"
#import "CommentModel.h"
#import "DeleteCommentVModel.h"
#import "CenterAction.h"

@interface NewsCommentCell ()<GetCommentDelegate>

@property (nonatomic, strong) CALayer *line;
@property (nonatomic, strong) UIImageView *Userlogo;
@property (nonatomic, strong) UILabel *Name;
@property (nonatomic, strong) UILabel *Commont;
@property (nonatomic, strong) UILabel *Date;

@property (nonatomic, strong) UIView  *Bg_view;
@property (nonatomic, strong) UILabel *Y_Commont;

@property (nonatomic, strong) UIButton *Usergoto;

@end

@implementation NewsCommentCell

+ (instancetype)cellForTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"NewsCommentCell";
    
    NewsCommentCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell)
    {
        cell = [[NewsCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.backgroundColor          = [UIColor whiteColor];
        
        [cell.contentView.layer addSublayer:cell.line];
        [cell.contentView addSubview:cell.Userlogo];
        [cell.contentView addSubview:cell.Name];
        [cell.contentView addSubview:cell.Commont];
        [cell.contentView addSubview:cell.Date];
        [cell.contentView addSubview:cell.Morebutton];
        [cell.contentView addSubview:cell.Bg_view];
        [cell.Bg_view addSubview:cell.Y_Commont];
        [cell.contentView addSubview:cell.Usergoto];
        
    }
    
    return cell;
}

- (void)setModel:(CommentModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    
    [_Userlogo sd_setImageWithURL:[NSURL URLWithString:_Model.logo] placeholderImage:[UIImage imageNamed:@"user_logo.png"]];
    _Name.text = _Model.name;
    _Commont.text = _Model.comment;
    _Date.text = [JonesClass timeZhuan:_Model.date];
    
    _Commont.frame = CGRectMake(70, 50, RECRWITCH-100, _Model.comment_height);
    _Date.frame = CGRectMake(70, 50 + _Model.comment_height, RECRWITCH-100, 20);
    
    if ([UserModel info].userid) {
        _Morebutton.hidden = NO;
    }else{
        _Morebutton.hidden = YES;
    }
    
    if ([_Model.to_comment_id intValue]>0) {
        _Bg_view.hidden = NO;
        _Y_Commont.hidden = NO;
        
        _Bg_view.frame=CGRectMake(70, 50 + _Model.comment_height + 20 + 10, RECRWITCH-100, _Model.to_comment_height);
        _Y_Commont.frame=CGRectMake(10, 0, RECRWITCH-120, _Model.to_comment_height);
        _Y_Commont.text=_Model.t_comment;
    }else{
        _Bg_view.hidden = YES;
        _Y_Commont.hidden = YES;
    }
}

- (CALayer *)line{
    if (!_line) {
        _line = [[CALayer alloc] init];
        _line.frame=CGRectMake(25, 0, RECRWITCH-50, 0.7);
        _line.backgroundColor=LineColor.CGColor;
    }
    return _line;
}

-(UIImageView *)Userlogo{
    if (!_Userlogo) {
        _Userlogo                     = [[UIImageView alloc] init];
        _Userlogo.layer.masksToBounds = YES;
        _Userlogo.layer.cornerRadius  = 17.5;
        _Userlogo.contentMode         = UIViewContentModeScaleAspectFill;
        _Userlogo.backgroundColor = backColor;
        _Userlogo.frame=CGRectMake(25, 15, 35, 35);
        [_Userlogo sd_setImageWithURL:[NSURL URLWithString:@"https://p1.meituan.net/dpdeal/4dc499a86562976bafb7474123bd323881371.jpg%40640w_1024h_1e_1l%7Cwatermark%3D1%26%26r%3D1%26p%3D9%26x%3D2%26y%3D2%26relative%3D1%26o%3D20"]];
    }
    return _Userlogo;
}

- (UIButton *)Usergoto{
    if (!_Usergoto) {
        _Usergoto = [[UIButton alloc] initWithFrame:CGRectMake(15, 5, 55, 55)];
        [_Usergoto addTarget:self action:@selector(gotousercenter:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _Usergoto;
}

-(UILabel *)Name{
    if (!_Name) {
        _Name           = [[UILabel alloc] init];
        _Name.font      = [UIFont systemFontOfSize:14];
        _Name.textColor = NewsTitleColor;
        _Name.text = @"山东分局";
        _Name.frame=CGRectMake(70, 15, RECRWITCH-100, 20);
    }
    return _Name;
}

-(UILabel *)Commont{
    if (!_Commont) {
        _Commont           = [[UILabel alloc] init];
        _Commont.font      = [UIFont systemFontOfSize:13];
        _Commont.textColor = NewsinfoColor;
        _Commont.frame = CGRectMake(70, 50, RECRWITCH-100, 30);
        _Commont.text = @"颜色不会换?教你1招,用手机轻松搞定!";
        _Commont.numberOfLines = 0;
    }
    return _Commont;
}

-(UILabel *)Date{
    if (!_Date) {
        _Date           = [[UILabel alloc] init];
        _Date.font      = [UIFont systemFontOfSize:12];
        _Date.textColor = NewsotherColor;
        _Date.frame = CGRectMake(70, 80, RECRWITCH-100, 20);
        _Date.text = @"20分钟前";
    }
    return _Date;
}

- (UIButton *)Morebutton{
    if (!_Morebutton) {
        _Morebutton = [[UIButton alloc] initWithFrame:CGRectMake(RECRWITCH-45, 15, 20, 20)];
        [_Morebutton setImage:[UIImage imageNamed:@"news_more"] forState:UIControlStateNormal];
        _Morebutton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_Morebutton addTarget:self action:@selector(moreshow:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _Morebutton;
}

-(UILabel *)Y_Commont{
    if (!_Y_Commont) {
        _Y_Commont           = [[UILabel alloc] init];
        _Y_Commont.font      = [UIFont systemFontOfSize:12];
        _Y_Commont.textColor = NewsinfoColor;
        _Y_Commont.frame = CGRectMake(70, 50, RECRWITCH-100, 30);
        _Y_Commont.numberOfLines = 0;
    }
    return _Y_Commont;
}

- (UIView *)Bg_view{
    if (!_Bg_view) {
        _Bg_view = [[UIView alloc] initWithFrame:CGRectMake(70, 50, RECRWITCH-100, 30)];
        _Bg_view.backgroundColor = backColor;
        _Bg_view.layer.masksToBounds = YES;
        _Bg_view.layer.cornerRadius = 3.0;
    }
    return _Bg_view;
}

- (void)moreshow:(UIButton *)sender{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    NSString *show_title;
    if ([[UserModel info].userid isEqualToString:_Model.userid]) {
        show_title = @"删除";
    }else{
        show_title = @"回复";
    }
    
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:show_title style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if ([[UserModel info].userid isEqualToString:_Model.userid]) {
            [self deletecomm];
        }else{
            self->_seleckblock(1);
        }
    }];
    [alertController addAction:sureAction];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    [_SuperVC presentViewController:alertController animated:YES completion:nil];
    
}

- (void)deletecomm{
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:_Model.userid forKey:@"userid"];
    [postdic setValue:_Model.news_id forKey:@"news_id"];
    [postdic setValue:_Model.comment_id forKey:@"comment_id"];
    
    DeleteCommentVModel *viewmdoel = [[DeleteCommentVModel alloc] init:postdic];
    viewmdoel.delegate=self;
}

- (void)DeleteCommentSucces:(int)value{
    if (value == 1) {
        _seleckblock(0);
    }
}

- (void)gotousercenter:(UIButton *)sender{
    
    UserModel *linmodel = [UserModel new];
    linmodel.userid = _Model.userid;
    linmodel.name = _Model.name;
    linmodel.logo = _Model.logo;
    linmodel.briday = _Model.briday;
    linmodel.sex = _Model.sex;
    
    [CenterAction Actiontodone:JonesGOTO_User_Center view:_SuperVC dictionary:@{@"model":linmodel}];
}

@end
